package com.maniu.bilirtmp.cameraxpush;

import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.maniu.bilirtmp.R;
import com.maniu.bilirtmp.live.LivePusher;


public class MainActivity extends AppCompatActivity {
    private final static int PERMISSION_REQUEST_CODE = 0x01;
    VideoChanel videoChanel;
    AudioChannel audioChannel;
    //H264码流
    private LivePusher livePusher;
    private TextureView textureView;
    //    private String url = "rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_345162489_81809986&key=03693092c85bd15a1d3fbbc227da0ad1&schedule=rtmp";
    private final String url = "rtmp://10.9.86.140:1935/livehime";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camerax);
        checkPermission();


    }

    public void checkPermission() {
        if (checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO
            }, PERMISSION_REQUEST_CODE);
        } else {
            initLivePusher();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            //请求码相同
            if (grantResults.length > 0 &&
                    (grantResults[0] != PackageManager.PERMISSION_GRANTED ||
                            grantResults[1] != PackageManager.PERMISSION_GRANTED ||
                            grantResults[2] != PackageManager.PERMISSION_GRANTED)) {
                //如果结果都存在，但是至少一个没请求成功，弹出提示
                Toast.makeText(this, "请同意必须的应用权限，否则无法正常使用该功能！", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length > 0) {
                //如果结果都存在，两个权限都申请成功，连接服务，启动录屏
                Toast.makeText(this, "权限申请成功，用户同意！", Toast.LENGTH_SHORT).show();
                initLivePusher();
            }
        }
    }

    private void initLivePusher() {
        textureView = findViewById(R.id.textureView);
        livePusher = new LivePusher(this, 800, 480, 800_000, 10, Camera.CameraInfo.CAMERA_FACING_BACK);
        //        链接B站
        livePusher.startLive(url);
        videoChanel = new VideoChanel(this, textureView, livePusher);
        audioChannel = new AudioChannel(44100, 2, livePusher);
    }

    public void switchCamera(View view) {
        livePusher.switchCamera();
    }

    public void startLive(View view) {
        videoChanel.startLive();
        audioChannel.start();
    }

    public void stopLive(View view) {
        livePusher.stopLive();
        audioChannel.stop();
        videoChanel.stopLive();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        livePusher.native_release();
    }
}
